package com.wwwc.index.servlet;

import java.net.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;

public class CheckMail {

    public static StringBuffer getMail (String server, String user, String pass) {
	StringBuffer sbf = new StringBuffer("<TABLE width=90% align=center border=1>");
	sbf.append("<TR><TD>From</TD><TD>Subject</TD>");
	sbf.append("<TD>Date</TD><TD>Size</TD></TR>");
	java.util.Properties properties = System.getProperties();
	Session session = Session.getInstance(properties, null);
	
	/*
	Provider[] providers = session.getProviders();
	for (int i =0; i<providers.length; i++) {
	    sbf.append("<BR>Providers["+i+"]="+providers[i]);
	}
	*/
	
	try {
	    Store store = session.getStore("pop3");
	    store.connect(server, user, pass);

	    Folder folder = store.getFolder("INBOX");
	    folder.open(Folder.READ_ONLY);

	    System.out.println("MessageCount="+folder.getMessageCount());
	    System.out.println("NewMessageCount="+folder.getNewMessageCount());
	    System.out.println("UnreadMessageCount="+folder.getUnreadMessageCount());

	    Flags fls = folder.getPermanentFlags();
	    if(fls == null) {
		System.out.println("fls == null");
	    }
	    else {
		Flags.Flag[] sfs = fls.getSystemFlags();
		System.out.println("sfs="+sfs.length);
	    }

	    Message[] messages = folder.getMessages();
	    BufferedReader in = null;
	    
	    for(int i=0; i<messages.length; i++) {
		Flags flags = messages[i].getFlags();
		if(flags == null) {
		    System.out.println("flags == null");
		}

		Flags.Flag[] sf = flags.getSystemFlags();

		if(sf == null) {
		    System.out.println("sf == null");
		}
		else {
		    for (int j = 0; j < sf.length; j++) {
			if (sf[j] == Flags.Flag.DELETED) {
			    System.out.println("["+j+"] DELETED message");
			}
			else if (sf[j] == Flags.Flag.SEEN) {
			    System.out.println("["+j+"] SEEN message");
			}
		    }
		}

		String[] uf = flags.getUserFlags();
		if(uf == null) {
		    System.out.println("uf == null");
		}
		else {
		    for (int j = 0; j<uf.length; j++) {
			System.out.println("Flag"+uf[j].toString());
		    }
		}

		sbf.append("<TR><TD>"+messages[i].getFrom()[0]+"</TD>");
		sbf.append("<TD>"+messages[i].getSubject()+"</TD>");
		sbf.append("<TD>"+messages[i].getSentDate()+"</TD>");
		sbf.append("<TD>"+messages[i].getMessageNumber()+"</TD></TR>");
		
		/*
		in  = new BufferedReader(new InputStreamReader(messages[i].getInputStream()));
		String temp;
		sbf.append("<TR><TD colspan=4>");
		while((temp = in.readLine()) != null) {
		    sbf.append("<BR>"+temp);
		}
		sbf.append("</TD></TR>");
		*/
	    }
	    
	    sbf.append("</TABLE>");
	    //in.close();
	    folder.close(false);
	    store.close();
	}
	catch (Exception e){
	    System.out.println("CheckMail:Error:101:"+e);
	}
	/*
	try { 
	    CheckMail t1 = new CheckMail();
	    int i = t1.checkMyMail(server, user, pass);
	    if (i==0) {
		sbf.append("<BR>No mail waiting.");
	    }
	    else {
		sbf.append("<BR>There " + (i==1?"is " :"are ") + i +" message" +(i==1?"":"s")+ " waiting.");
	    }
	}
	catch (Exception e) {
	    System.out.println("CheckMail:Error:102:"+e);
	}
	*/
	return sbf;
    }

    private void send(BufferedWriter out, String s) {
	try {
	    out.write(s+"\n");
	    out.flush();
	}
	catch (Exception e) {
	    System.out.println("CheckMail:send:Error:"+e);
	}
    }

    private String receive(BufferedReader in) {
	String in_string = null;
	try {
	    in_string = in.readLine();
	    if(in_string.startsWith("-ERR")) {
		in.close();
		in_string = "Error";
	    }
	}
	catch (Exception e) {
	    System.out.println("CheckMail:Error:103:"+e);
	}
	return in_string;
    }

    private int checkMyMail(String server, String user, String pass) {
	int nm = 0;
	try {
	    Socket s = new Socket(server, 110);
	    BufferedReader in  = new BufferedReader(new InputStreamReader(s.getInputStream()));
	    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
	
	    receive(in);
	    send(out, "USER " + user);
	    
	    receive(in);
	    send(out, "PASS " + pass);
	    
	    receive(in);
	    nm = GetNumberOfMessages(in, out);
	    in.close();
	    out.close();
	}
	catch (Exception e) {
	    System.out.println("CheckMail:Error:104:"+e);
	}
	return nm;
    }
    
    private int GetNumberOfMessages (BufferedReader in, BufferedWriter out) {
	int i = 0;
	String s = null;

	try {
	    send(out, "LIST");
	    receive(in);
	    
	    while((s = receive(in)) != null) {
		if (!(s.equals("."))) {
		    i++;
		}
		else {
		    break;
		    //return i;
		}
	    }
	    out.close();
	    in.close();
	}
	catch(Exception e) {
	    System.out.println("CheckMail:Error:105:"+e);
	}
	return i;
    }
}



